﻿using LabManagement.Data;
using LabManagement.Data.DataModel;
using OnlineLabManagement.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OnlineLabManagement.Areas.AdminConsole.Controllers
{
    [Authorize(Roles = "Admin")]  
    public class LabsController : DbControllerBase
    {
        //
        // GET: /AdminConsole/Labs/

        public ActionResult Index(int? page, int? pagesize)
        {
            var PageNumber = (page ?? 1);
            var PageSize = (pagesize ?? 5);
            var labs = this.DatabaseFactory.LabUtils.Labs();
            return View(labs);
        }
        //
        // GET: /AdminConsole/Labs/Create

        public ActionResult Create()
        {
            Lab lab = new Lab();
            ViewBag.LabCategories = this.DatabaseFactory.LabUtils.LabCategories();
            return View(lab);
        }

        //
        // POST: /AdminConsole/Labs/Create

        [HttpPost]
        public ActionResult Create(Lab lab)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newLabid = Guid.NewGuid();
                    IList<LabCategory> cats = new List<LabCategory>();
                    foreach (var cat in lab.LabCategories)
                    {
                        if (cat.Active)
                        {
                            cat.LabCategoryId = Guid.NewGuid();
                            cat.LabId = newLabid;
                            cats.Add(cat);
                        }
                       
                    }
                    lab.LabCategories = cats;
                    lab.LabId = newLabid;
                    this.DataBridge.Entry(lab).State = System.Data.Entity.EntityState.Added;
                    this.DataBridge.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                ViewBag.LabCategories = this.DatabaseFactory.LabUtils.LabCategories();
                return View(lab);
            }
        }

        //
        // GET: /AdminConsole/Labs/Edit/5

        public ActionResult Edit(Guid id)
        {
            var lab = this.DataBridge.Labs.Find(id);
            ViewBag.LabCategories = this.DatabaseFactory.LabUtils.LabCategories(id);
            return View(lab);
        }

        //
        // POST: /AdminConsole/Labs/Edit/5

        [HttpPost]
        public ActionResult Edit(Guid id, Lab lab)
        {
            try
            {
                var cats = lab.LabCategories;
                lab.LabCategories = null;
                this.DataBridge.Entry(lab).State = System.Data.Entity.EntityState.Modified;
                foreach (var cat in cats)
                {
                    if (cat.LabId.HasValue)
                    {
                        this.DataBridge.Entry(cat).State = System.Data.Entity.EntityState.Modified;
                    }
                    else if (cat.Active && !cat.LabId.HasValue)
                    {
                        cat.LabCategoryId = Guid.NewGuid();
                        cat.LabId = lab.LabId;
                        this.DataBridge.Entry(cat).State = System.Data.Entity.EntityState.Added;
                    }

                }
                this.DataBridge.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

      
        //
        // POST: /AdminConsole/Labs/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid id)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
